export default defineNuxtPlugin((nuxtApp) => {
  const router = useRouter();
  router.beforeEach((to, from, next) => {
    // 获取所有路由路径
    const routes = router.getRoutes().map((route) => route.path);
    // 检查当前路径是否匹配任何已定义的路由
    const isRouteMatched = routes.some((route) => {
      // 使用正则表达式来匹配路径
      const pattern = new RegExp(`^${route.replace("*", ".*")}$`);
      return pattern.test(to.path);
    });

    if (!isRouteMatched) {
      console.log("No matching route found, redirecting to home");
      // 如果没有匹配的路由，重定向到首页
      next("/");
    } else {
      next();
    }
  });
});
